package cn.com.jslh.boarpro.main.view.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseFragment;
import cn.com.jslh.boarpro.main.MainDetilActivity;
import cn.com.jslh.boarpro.main.contract.RecordTemplateContract;
import cn.com.jslh.boarpro.main.presenter.RecordTemplatePresenterImpl;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecordTemplateFragment extends BaseFragment implements RecordTemplateContract.View, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    @BindView(R.id.swipRefreshLayout)
    SwipeRefreshLayout swipRefreshLayout;
    @BindView(R.id.layout)
    LinearLayout layout;
    private RecordTemplateContract.Presenter presenter;

    public RecordTemplateFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new RecordTemplatePresenterImpl(getContext(), this);
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_record_template, container, false);
        unbinder = ButterKnife.bind(this, view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recycleview.setLayoutManager(linearLayoutManager);
        recycleview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        swipRefreshLayout.setOnRefreshListener(this);
        presenter.start();
        return view;
    }

    @Subscribe
    public void handleUpDataAction(MainDetilActivity.Action action) {
        if (isResumed() && ((MainDetilActivity) getContext()).getViewPage().getCurrentItem() == 0) {
            presenter.upAction();//上传数据
        }
    }

    @Subscribe
    public void callRefresh(String str) {
        if (str.equals("refresh")) {
            presenter.refresh();//刷新界面
        }
    }

    public static void reFresh() {
        EventBus.getDefault().post("refresh");
    }


    @Override
    public void setPresenter(RecordTemplateContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onRefresh() {
        //下拉刷新
        presenter.refresh();
    }

    @Override
    public void finshRefresh() {
        if (swipRefreshLayout.isRefreshing()) {
            swipRefreshLayout.setRefreshing(false);
        }
    }

    /**
     * 因为表头数据是根据第一次获取数据之后生成，所以需要在获取初始化数据之后再绑定RV
     */
    @Override
    public void finishLoadData() {
        presenter.setHeader(layout);
        presenter.bindRV(recycleview);
    }
}
